//
//  VKFollowController.m
//  V-Learn
//
//  Created by liubo on 2019/1/14.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKFollowController.h"
#import "VKFollowCell.h"
#import "VKFansModel.h"
@interface VKFollowController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) NSInteger pageNums;

@end

@implementation VKFollowController
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 76;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"关注";
    [self tableView];
    [self setupLeftRefresh];
}
//刷新
- (void)setupLeftRefresh
{   //下拉
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(setupLoadData)];
    //上拉
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(setupLoadMoreData)];
    
    [self.tableView.mj_header beginRefreshing];
}
- (void)setupLoadData{
    _page = 1;
    _pageNums = 10;
    [self setupLayout];
}
/**
 加载更多数据
 */
- (void)setupLoadMoreData{
    
    ++(_page);
    _pageNums = 10;
    [self setupLayout];
}//决定footer的状态
- (void)checkFooterState{
    
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)setupLayout{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载...";
    [hud show:YES];
    VKWeakSelf(self)
//    NSMutableDictionary *dic = @{@"token":[UserModel getUserModel].token,@"type":@"1"}.mutableCopy;
    NSMutableDictionary *dic = @{@"token":[UserModel getUserModel].token,@"type":@"1",@"page":[NSString stringWithFormat:@"%ld",(long)_page],@"num":[NSString stringWithFormat:@"%ld",(long)_pageNums]}.mutableCopy;
    [WYHXNetWorkService netWorkSerViceRequestWithGetUrl:WKStudyMyQueryFansOrAtten withPostParams:dic.mutableCopy withBlock:^(NSDictionary *result, NSError *error) {
        hud.hidden = YES;
        if ([result[@"code"]integerValue] == 200) {
            if (weakself.page == 1) {
                [self.dataArray removeAllObjects];
            }
            for (NSDictionary *dic in result[@"data"]) {
                VKFansModel *model = [VKFansModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            if ([result[@"data"] count] < 10) {
                //不能加载更多数据
                [self checkFooterState];
                [MBProgressHUD showSuccess:@"已加载全部数据" toView:self.view];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }

        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VKFollowCell *cell = [VKFollowCell cellWithTableView:tableView];
    
    cell.model = self.dataArray[indexPath.row];
    cell.rightBtn.tag = 1011+indexPath.row;
    [cell.rightBtn addTarget:self action:@selector(clickfollowBtn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma mark - 关注按钮
- (void)clickfollowBtn:(UIButton *)sender{
    NSInteger index = sender.tag-1011;
    sender.selected = !sender.selected;
    VKFansModel *model = self.dataArray[index];
    NSString *token = [UserModel getUserModel].token;
    if (sender.selected) {
        [WYHXNetWorkManager post:WKCancelFollowniuwa_WKURL params:@{@"token":token,@"attenUserId":model.userId} completionHandler:^(id responseObj, NSError *error) {
//            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
//                [self setupData:self.ageNum termnum:self.termNum];
                [sender setTitle:@"关注" forState:UIControlStateNormal];
                sender.layer.cornerRadius = 13;
                sender.layer.masksToBounds = YES;
                sender.backgroundColor = rgb(233,84,18);
                [sender setTitleColor:rgb(255,255,255) forState:UIControlStateNormal];

            }
        }];
    }else{
        [WYHXNetWorkManager post:WKFollowniuwa_WKURL params:@{@"token":token,@"attenUserId":model.userId} completionHandler:^(id responseObj, NSError *error) {
//            NSLog(@"%@",responseObj);
            NSString *code = responseObj[@"code"];
            if ([code integerValue]==200) {
                [sender setTitle:@"取消关注" forState:UIControlStateNormal];
                sender.layer.cornerRadius = 13;
                sender.layer.masksToBounds = YES;
                sender.backgroundColor = [UIColor whiteColor];
                [sender setTitleColor:rgb(233,84,18) forState:UIControlStateNormal];
                sender.layer.borderColor = rgb(233,84,18).CGColor;
                sender.layer.borderWidth = 1;
            }
        }];
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
